package com.common.commonlibrary.utils

import java.text.SimpleDateFormat
import java.util.*

object TimeUtils {

    private var time: Long = 0
    private const val yyyy_MM_dd_HH_mm_local = "yyyy年MM月dd日 HH:mm"

    /**
     * long time to string
     *
     * @param timeInMillis timeInMillis
     * @param dateFormat   dateFormat
     * @return String
     */
    fun getTime(timeInMillis: Long, dateFormat: SimpleDateFormat): String {
        return dateFormat.format(Date(timeInMillis))
    }

    /**
     * 双击事件
     *
     * @param interval 间隔 ms
     * @return
     */
    fun dblClick(interval: Long): Boolean {
        if (System.currentTimeMillis() - time in 0 until interval) {
            return true
        }
        time = System.currentTimeMillis()
        return false
    }

    /**
     * yyyy_MM_dd_HH_mm
     *
     * @param datestr
     * @return
     */
    private fun strToDateLoc_yyyy_MM_dd_HH_mm(datestr: String): Date? {
        var date: Date? = null
        try {
            val fmt = SimpleDateFormat(yyyy_MM_dd_HH_mm_local)
            date = fmt.parse(datestr)
        } catch (e: Exception) {
            return date
        }

        return date
    }

    /**
     * 2012年01月03日 12:55
     *
     * @param timeString
     * @return
     */
    fun getRecentTimeyyyy_MM_dd(timeString: String): String {
        var date: Date? = null
        try {
            date = Date(timeString.toLong())
        }catch (e: java.lang.Exception) {
            e.printStackTrace()
        }
        return getRecentTime(timeString, date)
    }

    /**
     * 聊天显示
     *
     * @param timeString
     * @return 三小时前、昨天等
     */
    private fun getRecentTime(timeString: String, date: Date?): String {
        if (date == null || timeString.isNullOrEmpty())
            return ""
        val cn_string = StringBuilder()
        val calendar = Calendar.getInstance()
        calendar.time = date
        val day = calendar.get(Calendar.DAY_OF_YEAR)         //月份中的日
        val week = calendar.get(Calendar.WEEK_OF_YEAR)          //年中周
        val hour = calendar.get(Calendar.HOUR_OF_DAY)           //时间

        val month = calendar.get(Calendar.MONTH) + 1         //月份.显示用
        var day_of_week = calendar.get(Calendar.DAY_OF_WEEK) - 1   //周中的日.显示用
        if (day_of_week == 0) day_of_week = 7
        val minute = calendar.get(Calendar.MINUTE)             //分钟.显示用

        val nowCalendar = Calendar.getInstance()
        nowCalendar.time = Date()
        val now_day = nowCalendar.get(Calendar.DAY_OF_YEAR)
        val now_week = nowCalendar.get(Calendar.WEEK_OF_YEAR)
        val now_hour = nowCalendar.get(Calendar.HOUR_OF_DAY)
        val now_minute = nowCalendar.get(Calendar.MINUTE)
        //时间字符串
        val hoursString = StringBuilder("" + hour)
        val minutesString = StringBuilder("" + minute)

        if (hour < 10)
            hoursString.insert(0, "0")
        if (minute < 10)
            minutesString.insert(0, "0")

        if (day == now_day && hour == now_hour && minute == now_minute) {
            cn_string.append("刚刚")
        } else if (day == now_day && hour == now_hour) {
            cn_string.append(Math.abs(now_minute - minute)).append("分钟前")
        } else if (now_day - day in 0..0 && now_hour - hour < 24 && now_hour - hour > 0) {
            cn_string.append(now_hour - hour).append("小时前")
        } else if (now_day == day) {
            cn_string.append("今天").append(hoursString).append(":").append(minutesString)
        } else if (now_day - day == 1)
            cn_string.append("昨天").append(hoursString).append(":").append(minutesString)
        else if (now_day - day == 2)
            cn_string.append("前天").append(hoursString).append(":").append(minutesString)
        else if (day - now_day == 1)
            cn_string.append("明天").append(hoursString).append(":").append(minutesString)
        else if (day - now_day == 2)
            cn_string.append("后天").append(hoursString).append(":").append(minutesString)
        else if (week == now_week && day > now_day || week - now_week == 1 && day_of_week == 7)
            cn_string.append("周").append(numToUpper(day_of_week)).append(" ").append(hoursString).append(":").append(
                minutesString
            )
        else if (week - now_week == 1)
            cn_string.append("下周").append(numToUpper(day_of_week)).append(" ").append(hoursString).append(":").append(
                minutesString
            )
        else {  //直接显示
            val monthString = StringBuilder("" + month)
            val _day = calendar.get(Calendar.DAY_OF_MONTH)         //月份中的日
            val dayString = StringBuilder("" + _day)
            if (month < 10)
                monthString.insert(0, "0")
            if (_day < 10)
                dayString.insert(0, "0")
            cn_string.append(monthString).append("月").append(dayString).append("日").append(hoursString).append(":")
                .append(minutesString)
        }
        return cn_string.toString()
    }

    /**
     * 将数字转化为大写
     *
     * @param num
     * @return
     */
    private fun numToUpper(num: Int): String {
        val u1 = arrayOf("〇", "一", "二", "三", "四", "五", "六", "日")
        val str = num.toString().toCharArray()
        var rstr = ""
        for (i in str.indices) {
            rstr += u1[Integer.parseInt(str[i] + "")]
        }
        return rstr
    }

    /**
     * 判断是否是同一个月
     */
    fun isSameMonth(t1: Date, t2: Date): Boolean{
        val format = SimpleDateFormat("yyyy-MM")
        return format.format(t1) == format.format(t2)
    }

    /**
     * 判断是否是同一天
     */
    fun isSameDay(t1: Date, t2: Date): Boolean{
        val format = SimpleDateFormat("yyyy-MM-dd")
        return format.format(t1) == format.format(t2)
    }

    /**
     * 比较两个时间的大小
     */
    fun compareTime(t1: Date, t2: Date): Boolean{
        return t1.time > t2.time
    }

    fun getMonth(t: Date): Int{
        val calendar = Calendar.getInstance()
        calendar.time = t
        return calendar.get(Calendar.MONTH)
    }

    /**
     * 格式化数据
     */
    fun formatDate(source: String, format: String): Date? {
        return try {
            val format = SimpleDateFormat(format)
            format.parse(source)
        }catch (e: java.lang.Exception) {
            null
        }
    }

    /**
     * 格式化数据
     */
    fun formatDate(time: Long, format: String): String? {
        return try {
            val format = SimpleDateFormat(format)
            format.format(time)
        }catch (e: java.lang.Exception) {
            null
        }
    }
}